//
//  MainViewController.swift
//  swft_weibo
//
//  Created by mac on 15/9/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor=UIColor.redColor()
        //创建所有子控制器
        addAllChildrenControllers()

        
    }
    /**
    在这里面添加一个plus按钮，38行添加按钮前，tabbar前面已经加入了所有的按钮了，因此plus会在所有按钮的最上层
    因此要在这个方法中添加
    
    :param: animated <#animated description#>
    */
    override func viewWillAppear(animated: Bool) {
    
        super.viewWillAppear(animated)
        //添加plus按钮
        addPlusBtn()

    }
    /**
    添加一个plus按钮
    */
    private func addPlusBtn(){
    
        plusBtn.center=CGPointMake(tabBar.bounds.size.width*0.5, tabBar.bounds.size.height*0.5)
        tabBar.addSubview(plusBtn)
    
    }

    
    /**
    创建所有的子控制器
    */
    private func addAllChildrenControllers(){
    
        //1.从网络上获取创建控制器的json文件
        let path=NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        
        let data=NSData(contentsOfFile: path!)
        
       
        do{
        
         let ctrArray=try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves)
            
            for ctr in ctrArray as! [[String:String]]
        {
            
            createOneController(ctr["vcName"]!, tabbarItemName: ctr["title"]!, tabbarImageName: ctr["imageName"]!)
            
        }
            
        }catch{

        }
 
    }
    /**
    创建一个控制器并把这个控制器放到tabbarcontroller中去
    
    :param: contrllerName   <#contrllerName description#>
    :param: tabbarItemName  <#tabbarItemName description#>
    :param: tabbarImageName <#tabbarImageName description#>
    */
    private func createOneController(contrllerName:String,tabbarItemName:String,tabbarImageName:String){
    
        //1.根据json数据中的控制器字符串，创建对应的类
        let oneClass=NSClassFromString("swft_weibo."+contrllerName) as! UIViewController.Type
        //2.创建控制器,设置对应的tabbaritem信息
        let oneCTR=oneClass.init()
        oneCTR.tabBarItem.title=tabbarItemName
        oneCTR.tabBarItem.image=UIImage(named: tabbarImageName)
        oneCTR.tabBarItem.selectedImage=UIImage(named:tabbarImageName+"_highlighted")
        
        oneCTR.navigationItem.title=tabbarItemName
        //3.将控制器添加进tabbar中去
        let nav = UINavigationController(rootViewController: oneCTR)
        
        addChildViewController(nav)
    }
    //懒加载加号按钮
    private lazy var plusBtn :UIButton={
        
        let btn=UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.sizeToFit()
        return btn
    
    }()

}


